package com.test.send_wxmessage.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@Component
public class Wxutil {

    //存储token
    private static String access_token = null;
    private static Date access_token_Invalid = null;



    /**
     * 获取接口访问凭证
     */
    public static String getAccess_token(String appid,String secret) {
        access_token = getAccessToken(appid,secret);
        return access_token;
    }

    //获取token
    private static String getAccessToken(String appid,String secret) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
        JSONObject jsonObject = processUrl(requestUrl, HttpMethod.GET);
        if (null != jsonObject) {
            String string = jsonObject.getString("access_token");
            int expires_in = jsonObject.getInteger("expires_in");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.SECOND, expires_in);
            access_token_Invalid = cal.getTime();
            return string;
        }
        return null;
    }


    public static JSONObject processUrl(String url, HttpMethod method) {
        RestTemplate restTemplate = new RestTemplate();
        // 进行网络请求,访问url接口
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, null, String.class);
        if (null != responseEntity && responseEntity.getStatusCode() == HttpStatus.OK) {
            JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
            return jsonObject;
        } else {
            log.error("获取token失败 errcode:{" + responseEntity.getStatusCode() + "}");
        }
        return null;
    }

    /**
     * 获取ticket
     */
    public static Map<String, Object> getTicket(String appid,String secret) {
        Map<String, Object> map = new HashMap<>();
        //获取token的url
        String requestUrl = PropertiesUtil.getValue("get_token") + appid + "&corpsecret=" + secret;
        //进行网络请求，请求微信接口
        JSONObject jsonObject = Wxutil.processUrl(requestUrl, HttpMethod.GET);
        //将获取到的token信息保存到map中
        String access_token = jsonObject.getString("access_token");
        //获取ticket
        String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=" + access_token;
        JSONObject object = Wxutil.processUrl(url, HttpMethod.GET);

        String ticket = object.getString("ticket");
        String expires_in = jsonObject.getString("expires_in");
        map.put("access_token", access_token);
        map.put("expires_in", expires_in);
        map.put("ticket", ticket);
        return map;
    }


}
